#include "cmdpet.h"

#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"
#include "../cmd/cmdapi.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_GET_PET_INFO, CCmdGetPetInfo);

int CCmdGetPetInfo::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_GET_PET_INFO||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_GET_PET_INFO,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("get ReqPetInfo GID: %u", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdGetPetInfo::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iGidIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdGetPetInfo info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iGidIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iGidIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdGetPetInfo info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex];
    CUserPet& stUserPet = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex]._stUserPet;

    //检查系统是否开启
    if (!pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_3, stPlayerBaseInfo, stPlayerOnlineData))
    {
        return AnsClients(ERROR_SYS_NOT_OPEN);
    }

    stUserPet.m_bCanExplore = pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_24, stPlayerBaseInfo, stPlayerOnlineData);

    iRet = stUserPet.PetIsActive();
    if( iRet==0 )
    {
        iRet = pstMainCtrl->m_stPetMgr.GeneratePet(stUserPet, stPlayerBaseInfo._ucCareer);
        if( iRet!=RET_OK )
        return AnsClients(RET_SYSTEM_FAIL);
    }

    pstMainCtrl->m_stTaskMgr.TriggerDoSomeOpt(stPlayerOnlineData._stUserTask, TASK_OPT_ID_122, 1);
    
    pstMainCtrl->m_stPetMgr.GetPetInfo(stUserPet, stAns);

    return AnsClients(RET_OK);
}

int CCmdGetPetInfo::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("pet enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

//////////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_PET_TRAIN, CCmdPetTrain);

int CCmdPetTrain::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_PET_TRAIN ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_PET_TRAIN,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("req pet train:[%u]", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdPetTrain::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_EX1_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_EX1_INFO(uiGID);

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user detail info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerDetailInfo& stPlayerDetailInfo = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user detail ex1 info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerDetailInfoEx1& stPlayerDetailEx1Info = pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

    int iGidIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdPetTrain info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex];
    CUserPet& stUserPet = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex]._stUserPet;
    int iOrgPetLevel = stUserPet.GetPetLevel();
    int iOrgPetStar = stUserPet.GetPetStar();

    //检查系统是否开启
    short usRoleLevel = 0;
    pstMainCtrl->m_stRoleMgr.GetRoleLevel(stReq.gid(), usRoleLevel);
    if (!pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_3, stPlayerBaseInfo, stPlayerOnlineData))
    {
        return AnsClients(ERROR_SYS_NOT_OPEN);
    }

    stUserPet.m_bCanExplore = pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_24, stPlayerBaseInfo, stPlayerOnlineData);

    int iTrainType = stReq.train_type(); //取得宠物培养类型
    //检查满级 && 检查是否有果实未领取
    if( iTrainType!=PET_TRAIN_TYPE_4 )//果实
    {
        iRet = pstMainCtrl->m_stPetMgr.CheckPetTrain(stUserPet);
        if( iRet!=RET_OK )
        {
            return AnsClients(iRet);
        }
    }
    
    if( iTrainType==PET_TRAIN_TYPE_1 || iTrainType==PET_TRAIN_TYPE_2 )//皇家口粮 //金币
    {
        int iKouLiang = stReq.nums(); //口粮数量,只能为1和50
        int iGridPos = stReq.grid_pos(); //背包格子位置
        iRet = pstMainCtrl->m_stPetMgr.PetTrainKouLiang(stUserPet, iKouLiang, iGridPos, stAns);

        if (RET_OK == iRet)
        {
            pstMainCtrl->m_stTaskMgr.TriggerDoSomeOpt(stPlayerOnlineData._stUserTask, TASK_OPT_ID_131, 1);
        }
        
    }
    /*else if( iTrainType==PET_TRAIN_TYPE_2 )//金币
    {
        int iGoldNum = stReq.nums(); //金币数量
        iRet = pstMainCtrl->m_stPetMgr.PetTrainGold(stUserPet, iGoldNum);
        //iRet = pstMainCtrl->m_stPetMgr.PetTrainKouLiang(stUserPet, iGoldNum, iGridPos, stAns);
    }*/
    else if( iTrainType==PET_TRAIN_TYPE_3 )//银币
    {
        int iSilverNum = stReq.nums(); //银币数量
        iRet = pstMainCtrl->m_stPetMgr.PetTrainSilver(stUserPet, iSilverNum, stAns);
    }
    else if( iTrainType==PET_TRAIN_TYPE_4 )//果实
    {
        int iFruitNum = stReq.nums(); //果实数量
        int iGridPos = stReq.grid_pos(); //背包格子位置
        iRet = pstMainCtrl->m_stPetMgr.PetTrainFruit(stUserPet, iFruitNum, iGridPos);

        if (RET_OK == iRet)
        {
            pstMainCtrl->m_stNotifyMgr.Notify(stPlayerBaseInfo._uiGID, NOTIFY_PARTNER_ATTR | NOTIFY_ROLE_ATTR);
        }
    }

    //触发任务
    if (RET_OK == iRet)
    {
        pstMainCtrl->m_stTaskMgr.TriggerDoSomeOpt(stPlayerOnlineData._stUserTask, TASK_OPT_ID_112, 1);

        if (iOrgPetLevel != stUserPet.GetPetLevel() || iOrgPetStar != stUserPet.GetPetStar())
        {
            pstMainCtrl->m_stMatrixMgr.ReCalcMainObjAttr(stPlayerBaseInfo,
                stPlayerDetailInfo, stPlayerOnlineData, stPlayerDetailEx1Info);
            pstMainCtrl->m_stMatrixMgr.ReCalcMainFighting(stPlayerBaseInfo, stPlayerDetailInfo, stPlayerOnlineData);

            //新手目标任务
            pstMainCtrl->m_stNewbieTaskMgr.Trigger(stPlayerDetailEx1Info._stNewbieTaskUserData, uiGID, NEWBIE_TASK_COND_2, 
                                                stUserPet.GetPetLevel() * 10 + stUserPet.GetPetStar());

            //精彩活动
            pstMainCtrl->m_stActMgr.TriggerPetLevelUp(stPlayerDetailInfo._stSplendidAct, uiGID, stUserPet.GetPetLevel());

            pstMainCtrl->m_stTaskMgr.TriggerPetLevelUp(stPlayerOnlineData._stUserTask, stUserPet.GetPetLevel() * 10 + stUserPet.GetPetStar());
        }
        
        if (iOrgPetLevel != stUserPet.GetPetLevel() && stUserPet.GetPetLevel() >= 2)
        {
            MsgTips stTips;
            stTips.set_type(1006);
            stTips.add_tip_argument(CMainCtrl::m_pstMain->m_stRoleMgr.GetRoleName(stUserPet.GetUserGid()));
            stTips.add_tip_argument(stUserPet.GetPetName());
            stTips.add_tip_argument(CTStringUtils::IntToString(stUserPet.GetPetLevel()));
            CMainCtrl::m_pstMain->SendOnlineTips(stTips);
        }

        if (stAns.crit_big() > 0)
        {
            //跑马灯
            MsgTips stTips;
            stTips.set_type(1007);
            stTips.add_tip_argument(CMainCtrl::m_pstMain->m_stRoleMgr.GetRoleName(stUserPet.GetUserGid()));
            stTips.add_tip_argument(stUserPet.GetPetName());
            CMainCtrl::m_pstMain->SendOnlineTips(stTips);
        }
        
    }
    
    //设置返回值
    stAns.set_ret( iRet );

    return AnsClients(iRet);
}

int CCmdPetTrain::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("pet enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);

}

/////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_PET_OTHER, CCmdPetOther);

int CCmdPetOther::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_PET_OTHER ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_PET_OTHER,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("pet req other gid:[%u]", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    bHasCheckSenWord = false;
    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdPetOther::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_EX1_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_EX1_INFO(uiGID);

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user player base info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user detail info error, uid = %u", uiGID));
        return Done(RET_SYSTEM_FAIL);
    }
    CPlayerDetailInfo& stPlayerDetailInfo = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user detail ex1 info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerDetailInfoEx1& stPlayerDetailEx1Info = pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

    int iGidIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdGetPetInfo info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CUserPet& stUserPet = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex]._stUserPet;
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex];

    //检查系统是否开启
    short usRoleLevel = 0;
    pstMainCtrl->m_stRoleMgr.GetRoleLevel(stReq.gid(), usRoleLevel);
    if (!pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_3, stPlayerBaseInfo, stPlayerOnlineData))
    {
        return AnsClients(ERROR_SYS_NOT_OPEN);
    }

    stUserPet.m_bCanExplore = pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_24, stPlayerBaseInfo, stPlayerOnlineData);
    
    int iReqType = stReq.req_type();
    if( iReqType == CONST_PET_EXPLORE )//宠物探险 
    {
        //检查系统是否开启
        if (!pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_24, stPlayerBaseInfo, stPlayerOnlineData))
        {
            return AnsClients(ERROR_SYS_NOT_OPEN);
        }

        iRet = pstMainCtrl->m_stPetMgr.PetExplore(stUserPet, stAns);
    }
    else if( iReqType == CONST_PET_CHANGE_STYLE )//切换宠物形态
    {
        int iPetNewStyle = stReq.new_style();
        iRet = pstMainCtrl->m_stPetMgr.PetChangeStyle(stUserPet, iPetNewStyle);
        if( iRet==RET_OK )
        {
            pstMainCtrl->m_stNotifyMgr.Notify(stPlayerBaseInfo._uiGID, NOTIFY_PARTNER_ATTR | NOTIFY_ROLE_ATTR);

            //通知地图用户
            pstMainCtrl->m_stNotifyMgr.Notify(uiGID, NOTIFY_MAP_INFO);
        }
    }
    else if( iReqType == CONST_PET_RENAME )//宠物改名
    {
        CTSizeString<PETNAME_LEN> szPetName;
        szPetName.Set( "%s", stReq.pet_new_name().c_str() );

        if (!bHasCheckSenWord)
        {
            bHasCheckSenWord = true;
            
            iRet = pstMainCtrl->m_stPetMgr.CheckNewPetName(szPetName);
            if (iRet)
            {
                return AnsClients(iRet);
            }
            
            //在平台检查
            if (stPlayerOnlineData._bIsTencentLogin)
            {
                CCmdApi* pstCmd = (CCmdApi*)CT_CMDFACTORY->CreateCmd(ID_MAINAPI_TAPI);
                if (!pstCmd)
                {
                    CT_WARNING(("create cmd error"));
                    return AnsClients(RET_SYS_ERROR);
                }

                pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
                pstCmd->stReq._ucReqType = CReqApi::API_TENCENT;
                pstCmd->stReq._szTApiName.Set("/v3/csec/word_filter");
                pstCmd->stReq._stTApiKeyValue = stPlayerOnlineData._stTencentApiKeyValue;
                pstCmd->stReq._stTApiKeyValue.SetValue("content", szPetName());

                uint64 u64MsgID = CT_UINT64_MAKE(uiGID, pstMainCtrl->m_iNow);
                pstCmd->stReq._stTApiKeyValue.SetValue("msgid", CTStringUtils::Uint64ToString(u64MsgID, 16));

                RegisterChild(pstCmd, true);
                return pstCmd->Do(NULL);
            }  
        }
        else
        {
            if (m_pstChild && ID_MAINAPI_TAPI == m_pstChild->GetCmdID())
            {
                CCmdApi* pstCmd = (CCmdApi*)m_pstChild;
                if (pstCmd->stReq._szTApiName == "/v3/csec/word_filter")
                {
                    if (atoi(pstCmd->stAns._stTApiKeyValue.GetValue("is_dirty")) != 0)
                    {
                        return AnsClients(ERROR_HAS_SEN_WORD);
                    }
                }
            }
        }

        iRet = stUserPet.PetRename(szPetName);
    }
    else if( iReqType == CONST_PET_UPDATE_LEVEL )//宠物升等阶
    {
        iRet = pstMainCtrl->m_stPetMgr.PetReqUpdateLevel(stUserPet);
        if( iRet==RET_OK )
        {
            pstMainCtrl->m_stNotifyMgr.Notify(stPlayerBaseInfo._uiGID, NOTIFY_PARTNER_ATTR | NOTIFY_ROLE_ATTR);

            pstMainCtrl->m_stActMgr.TriggerPetLevelUp(stPlayerDetailInfo._stSplendidAct, uiGID, stUserPet.GetPetLevel());
            pstMainCtrl->m_stTaskMgr.TriggerPetLevelUp(stPlayerOnlineData._stUserTask, stUserPet.GetPetLevel() * 10 + stUserPet.GetPetStar());

            //触发新手目标
            pstMainCtrl->m_stNewbieTaskMgr.Trigger(stPlayerDetailEx1Info._stNewbieTaskUserData, uiGID, NEWBIE_TASK_COND_2, 
                stUserPet.GetPetLevel() * 10 + stUserPet.GetPetStar());
        }
    }
    else if( iReqType == CONST_PET_RECV_FRUIT )//领取果实
    {
        iRet = pstMainCtrl->m_stPetMgr.PetRecvFruit(stUserPet);
    }
    else if( iReqType == CONST_PET_CLEAR_CD_TIME )//清除cd时间
    {
        iRet = pstMainCtrl->m_stPetMgr.PetClearCDTime(stUserPet);
    }
    else if (iReqType == CONST_PET_ENABLE_NEW_STYLE) //激活猫头鹰形态
    {
        iRet = pstMainCtrl->m_stPetMgr.PetEnableNewStyle(stUserPet, PET_STATUS_TYPE_CAT);
        stAns.set_max_style_id(stUserPet.GetMaxStyleID());
        pstMainCtrl->m_stNotifyMgr.Notify(uiGID, NOTIFY_PARTNER_ATTR | NOTIFY_ROLE_ATTR);
    }

    stAns.set_req_type( iReqType );
    stAns.set_ret( iRet );
    stAns.set_gid( uiGID );
        
    return AnsClients(iRet);
}

int CCmdPetOther::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("pet enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}
